# +_+ coding:utf-8 +_+
import json
import config
import request
import time

__author__ = 'cz'


class KLine(object):
    high = 0
    low = 0
    start = 0
    end = 0
    trade = 0
    ts = time.time()
    b2l = 0

    def __init__(self, ts, h, l, s, e, t, bl):
        self.ts = time.localtime(ts/1000)
        self.high = h
        self.low = l
        self.start = s
        self.end = e
        self.trade = t
        self.b2l = bl

        pass

    def to_string(self):
        ts = time.strftime("%Y%m%d %H:%M:%S ", self.ts)
        s = "[{0}]:high:{1} low:{2} start:{3} end:{4} trade:{5}".format(
            ts, self.high, self.low, self.start, self.end, self.trade)
        return s


class KLineRequest(request.Request):
    def __init__(self):
        super(KLineRequest, self).__init__()
        self.set_host(config.svr_host)
        self.set_path(config.svr_path + "/future_kline.do")
        self.set_headers({"Content-type": "application/x-www-form-urlencoded"})
        param = dict()
        param["symbol"] = "btc_usd"
        param["contract_type"] = "quarter"
        param["app_key"] = config.app_key
        param["type"] = "5min"
        param["size"] = 1
        param["since"] = int(time.time())
        self.set_parameters(param)

    #symbol: btc_usd  contract_type: this_week/quarter   type: 1min/5min/ size:1/10x since:since time
    def setqueryparam(self, symbol, contract_type, type, size, since):
        param = dict()
        param["symbol"] = symbol
        param["contract_type"] = contract_type
        param["app_key"] = config.app_key
        param["type"] = type
        param["size"] = size
        param["since"] = since
        self.set_parameters(param)


def query():
    req = KLineRequest()
    rsp = req.do_request()
    body = rsp.body()
    body_json = json.loads(body)
    if type(body_json) == "dict" and body_json.has_key("error_code"):
        print("kline query error_code")
        return None
    the_kl = body_json[0]
    if not isinstance(the_kl, list) :
        print("line query None")	
        return None
    kline = KLine(the_kl[0],
                  the_kl[2],
                  the_kl[3],
                  the_kl[1],
                  the_kl[4],
                  the_kl[5],
                  the_kl[6])
    return kline

def querybyparam(symbol, contract_type, type, size, since):   
    req = KLineRequest()
    req.setqueryparam(symbol, contract_type, type, size, since)
    rsp = req.do_request()
    body = rsp.body()
    body_json = json.loads(body)
    #if type(body_json) == dict and body_json.has_key("error_code"):
    #    return None
    #the_kls = body_json[0]
    the_kls = body_json
    klines = list()
    print(the_kls)
    for item in the_kls:
        kline = KLine(item[0],
                      item[2],
                      item[3],
                      item[1],
                      item[4],
                      item[5],
                      item[6])
        klines.append(kline)              
    return klines    